/**
 * created by 小卷毛, 2022-08-06
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.other

import android.content.Context
import com.amap.api.convertArrList
import com.amap.api.convertToLatLng
import com.amap.api.maps.AMap
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.PolylineOptions
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.route.RidePath
import com.rzico.sbl.R
import overlay.RouteOverlay

/**
 * 描 述：
 * 作 者：小卷毛  2022-08-06 10:59
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class DeliverRouteOverlay(
    context: Context,
    aMap: AMap,
    private val path: RidePath,
    start: LatLonPoint,
    end: LatLonPoint
) : RouteOverlay(context) {

    private lateinit var mPolylineOptions: PolylineOptions

    init {
        mAMap = aMap
        startPoint = start.convertToLatLng()
        endPoint = end.convertToLatLng()
    }

    /**
     * 添加骑行路线到地图中。
     */
    fun addToMap() {
        initPolylineOptions()

        try {
            val firstPoint = path.polyline.first()
            val lastPoint = path.polyline.last()

            // 补全起点空隙
            if (firstPoint.latitude != startPoint.latitude
                || firstPoint.longitude != startPoint.longitude
            ) addRidePolyLine(startPoint)

            // 线段点
            addRidePolyLines()

            // 补全终点空隙
            if (lastPoint.latitude != endPoint.latitude
                || lastPoint.longitude != endPoint.longitude
            ) addRidePolyLine(endPoint)

            addPolyLine(mPolylineOptions)
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    /**
     * 添加单个线段点
     */
    private fun addRidePolyLine(point: LatLng) {
        mPolylineOptions.add(point)
    }

    /**
     * 添加所有线段点
     */
    private fun addRidePolyLines() {
        mPolylineOptions.addAll(path.polyline.convertArrList())
    }

    /**
     * 初始化线段属性
     */
    private fun initPolylineOptions() {
        mPolylineOptions = PolylineOptions().apply {
            color(driveColor)
            customTexture = BitmapDescriptorFactory.fromResource(R.mipmap.icon_order16)
            width(routeWidth)
        }
    }

}